package org.irri.smta.scheduler.service;

import javax.inject.Inject;
import javax.inject.Named;

import org.irri.smta.scheduler.Worker;
import org.irri.smta.util.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

@Named("schedulerService")
public class SchedulerService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(SchedulerService.class);

	//private static final long DELAY_1_MINUTE_IN_MILLIS = 60 * 1000; 
	//private static final long DELAY_5_MINUTES_IN_MILLIS = 5 * 60 * 1000;
	//private static final long DELAY_10_MINUTES_IN_MILLIS = 10 * 60 * 1000;
	//private static final long DELAY_15_MINUTES_IN_MILLIS = 15 * 60 * 1000;
	private static final long DELAY_20_MINUTES_IN_MILLIS = 20 * 60 * 1000;
	//private static final long DELAY_30_MINUTES_IN_MILLIS = 30 * 60 * 1000;
	//private static final long DELAY_60_MINUTES_IN_MILLIS = 60 * 60 * 1000;
	
	@Inject
	@Named("configLoaderWorker")
	private Worker configLoaderWorker;

	@Inject
	@Named("globalBeanLoaderWorker")
	private Worker globalBeanLoaderWorker;

	/**
	 * Runs the configLoaderWorker task.
	 */
	@Scheduled(fixedDelay = DELAY_20_MINUTES_IN_MILLIS)
	public void runConfigLoaderWorker() {
		LOGGER.debug("runConfigLoaderWorker()");
		if (configLoaderWorker != null) {
			configLoaderWorker.execute();
			SystemUtils.logMemoryStats();
		}
	}

	/**
	 * Runs the globalBeanLoaderWorker task.
	 */
	@Scheduled(fixedDelay = DELAY_20_MINUTES_IN_MILLIS)
	public void runGlobalBeanLoaderWorker() {
		LOGGER.debug("runGlobalBeanLoaderWorker()");
		if (globalBeanLoaderWorker != null) {
			globalBeanLoaderWorker.execute();
			SystemUtils.logMemoryStats();
		}
	}

}
